﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bitirme.Controllers;
using Bitirme.DAL.Entities;

namespace Bitirme.Views
{
    public partial class UpdateProject : System.Web.UI.Page
    {
        Project project;
        WorkPackageHandler workPackageHandler;
        int projectid = -1;

        private void FillData()
        {
            ProjectManagerHandler managerHandler = new ProjectManagerHandler();

            PMDropDownList.DataSource = managerHandler.GetAllProjectManagers();
            PMDropDownList.DataTextField = "Name";
            PMDropDownList.DataValueField = "ID";
            PMDropDownList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string iD = Request.QueryString["id"];
            projectid = Int32.Parse(iD);
            FillData();
            ProjectHandler projectHandler = new ProjectHandler();
            workPackageHandler = new WorkPackageHandler();
            project = projectHandler.SelectProjectByID(projectid);
            ProjectName.Text = project.Name;
            ProjectSubject.Text = project.Subject;
            ProjectDetail.Text = project.Details;
            ProjectCost.Text = project.Cost.ToString();
            ProjectDuration.Text = project.Duration.ToString();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            ProjectHandler projectHandler = new ProjectHandler();
            string Name = NameTextBox.Text;
            string Subject = SubjectTextBox.Text;
            string Details = DetailsTextBox.Text;
            int Duration = Convert.ToInt32(DurationTextBox.Text);
            int ProjectState = Convert.ToInt32(ProjectStateList.SelectedItem.Value);
            decimal Cost = Convert.ToDecimal(CostTextBox.Text);
            int ProjectManagerID = Convert.ToInt32(PMDropDownList.SelectedItem.Value);
            string StartDate = startdate.Text;
            string EndDate = enddate.Text;

            int intResult;
            try
            {

                intResult = projectHandler.UpdateProjectByID(projectid, Name, Subject, Details, ProjectState, Duration, Cost, ProjectManagerID, StartDate, EndDate);

                if (intResult > 0)

                    lblMessage.Text = "Proje güncellendi. :" + intResult;

                else

                    lblMessage.Text = "Hata: Proje güncellenemedi!";



            }

            catch (Exception ee)
            {

                lblMessage.Text = ee.Message.ToString();

            }

            finally
            {

                projectHandler = null;

            }


        }
    }
}